package com.pureut.quality.controller;


import com.pureut.common.core.constant.Constants;
import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.quality.domain.dto.QualityBadCategoryDto;
import com.pureut.quality.domain.vo.QualityBadCategoryVo;
import com.pureut.quality.export.QualityBadCategoryExport;
import com.pureut.quality.export.QualityInspectionExport;
import com.pureut.quality.service.IQualityBadCategoryService;
import com.pureut.quality.util.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Description 不良类别控制层
 * @Param
 * @Date 23-02-02
 * @Author hl
 **/
@RestController
@RequestMapping("/badCategory")
public class QualityBadCategoryController extends BaseController {

    @Resource
    private IQualityBadCategoryService qualityBadCategoryService;

    /**
     * 不良类别
     */
    @RequiresPermissions("quality:verifyconfig:badcategory:list")
    @GetMapping("/list")
    public TableDataInfo list(QualityBadCategoryVo qualityBadCategoryVo)
    {
        startPage();
        List<QualityBadCategoryDto> list = qualityBadCategoryService.selectBadCategoryList(qualityBadCategoryVo);
        return getDataTable(list);
    }

    /**
     * 新增不良类别
     */
    @RequiresPermissions("quality:verifyconfig:badcategory:add")
    @Log(title = "不良类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody QualityBadCategoryVo qualityBadCategoryVo) throws Exception {
        return toAjax(qualityBadCategoryService.insertBadCategory(qualityBadCategoryVo));
    }

    /**
     * 批量删除不良类别
     */
    @RequiresPermissions("quality:verifyconfig:badcategory:remove")
    @Log(title = "不良类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{badId}")
    public AjaxResult remove(@PathVariable("badId") String badId){
        return toAjax(qualityBadCategoryService.deleteBadCategoryById(badId));
    }

    /**
     * 根据编号获取详细信息
     */
    @RequiresPermissions("quality:verifyconfig:badcategory:query")
    @GetMapping(value = "/{badId}")
    public AjaxResult getInfo(@PathVariable long badId)
    {
        return AjaxResult.success(qualityBadCategoryService.selectBadCategoryById(badId));
    }

    /**
     * 修改不良类别
     */
    @RequiresPermissions("quality:verifyconfig:badcategory:edit")
    @Log(title = "不良类别", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody QualityBadCategoryVo qualityBadCategoryVo){
        return toAjax(qualityBadCategoryService.updateBadCategoryEdit(qualityBadCategoryVo));
    }

    /**
     * 生成不良类别导入模板
     */
    @PostMapping("/downTemplate")
    public void downTemplate(HttpServletResponse response) throws IOException {
        String name = "不良类别";
        String fileName = Constants.STATIC_URL + "导入" + name + ".xlsx";
        File file = new File(fileName);
        ByteArrayOutputStream baos = null;
        ServletOutputStream out = null;
        try {

            InputStream inStream=new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len;
            baos = new ByteArrayOutputStream();
            while ((len = inStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode("导入不良类别.xlsx","UTF-8"));
            out = response.getOutputStream();
            out.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baos.flush();
            baos.close();
            out.flush();
            out.close();
        }
    }

    /**
     * 不良类别信息导入
     */
    @RequiresPermissions("quality:verifyconfig:badcategory:import")
    @Log(title = "不良类别导入", businessType = BusinessType.INSERT)
    @PostMapping("/qualityInspectionImport")
    public AjaxResult qualityInspectionImport(MultipartFile file) throws Exception {
        ExcelUtil<QualityBadCategoryExport> excelUtil = new ExcelUtil<QualityBadCategoryExport>(QualityBadCategoryExport.class);
        List<QualityBadCategoryExport> coCustomer = excelUtil.importExcel(file.getInputStream());
        return AjaxResult.success(qualityBadCategoryService.importMouldData(coCustomer));
    }

}
